package com.duqc.tag.service.impl.strategy;

import cn.hutool.json.JSONUtil;
import com.duqc.tag.entity.ClassificationRule;
import com.duqc.tag.entity.classification.AttributeRule;
import com.duqc.tag.entity.enumeration.TagTypeEnum;
import com.duqc.util.JdbcConnectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

/**
 * @ClassName TagDataAttributeRule
 * @Description TagDataAttributeRule
 * @Author duqc
 * @Date 2023/2/9
 **/
@Slf4j
@Component
@TagStrategy
public class TagDataAttributeRule implements TagDataStrategy {

    @Resource
    private JdbcConnectionUtil jdbcConnectionUtil;

    /**
     * 返回 type
     *
     * @return String
     */
    @Override
    public String getType() {
        return String.valueOf(TagTypeEnum.ATTRIBUTE_RULE.getTagType());
    }

    /**
     * 获取标签数据
     *
     * @param objects  规则信息
     * @param relation 规则关系
     * @return List
     */
    @Override
    public List<String> calculationTag(List<ClassificationRule> objects, String relation) {
        List<String> sql = new ArrayList<>();
        objects.forEach(info -> {
            List<AttributeRule> attributeRules = JSONUtil.toList(info.getRules(), AttributeRule.class);
            //sql 拼接
            List<String> sqlList = buildSql(attributeRules);
            //todo or用union and用join
//            if ("or".equals(info.getRelation())) {
            unionSql(sqlList, sql);
//            }
        });
        List<String> attributeSql = new ArrayList<>();
        if ("and".equals(relation)) {
            TagDataCommon.joinSql(sql, attributeSql);
        }
        log.info(attributeSql.get(0));
        List<String> data = jdbcConnectionUtil.getData("SELECT\n" +
                "\t\tattribute_value \n" +
                "\tFROM\n" +
                "\t\tevent_list \n" +
                "\tWHERE\n" +
                "\t\tattribute_id = \"$element_name\" \n" +
                "\t\tAND attribute_group_id IN ( SELECT attribute_group_id FROM event_list WHERE attribute_id = \"$browser_version\" AND attribute_value = \"10.0.01\" GROUP BY attribute_group_id ) \n" +
                "\tGROUP BY\n" +
                "\t\tattribute_value ");
        return data;
    }

    /**
     * union sql拼接
     *
     * @param sqlList sql
     * @param sql     返回sql
     */
    private void unionSql(List<String> sqlList, List<String> sql) {
        StringJoiner stringJoiner = new StringJoiner(" UNION ");
        sqlList.forEach(stringJoiner::add);
        sql.add(stringJoiner.toString());
    }

    /**
     * 拼接sql
     *
     * @param attributeRules 规则信息
     */
    private List<String> buildSql(List<AttributeRule> attributeRules) {
        List<String> attributeSql = new ArrayList<>();
        attributeRules.forEach(info -> {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("SELECT attribute_value FROM event_list WHERE attribute_id = \"$element_name\" AND attribute_group_id IN (SELECT attribute_group_id FROM event_list WHERE attribute_id = \"");
            stringBuilder.append(info.getAttributeId())
                    .append("\" AND attribute_value = \"")
                    .append(info.getValue())
                    .append("\" GROUP BY attribute_group_id) GROUP BY attribute_value");
            attributeSql.add(stringBuilder.toString());
        });
        return attributeSql;
    }

}
